主题
从内存加载加密模型包 - YoloLoadModelMemory
函数简介
从内存缓冲区加载欧拉加密包并解密加载。
接口名称
YoloLoadModelMemoryDLL 调用
long YoloLoadModelMemory(long ola, long memoryAddr, int size, string password, int inferenceDevice);参数说明
| 参数名 | 类型 | 说明 |
|---|---|---|
| ola | 长整数型 | OLAPlug 对象指针,由 CreateCOLAPlugInterFace 生成。 |
| memoryAddr | 长整数型 | 加密包内存首地址。 |
| size | 整数型 | 加密包字节长度。 |
| password | 字符串 | 解密密码。 |
| inferenceDevice | 整数型 | inferenceDevice:-1 表示 CPU;0 及以上表示 GPU 索引。GPU 不可用时可能自动回退 CPU(以实际 ExecutionProvider 为准)。 |
示例
以下示例演示 读入欧拉加密包 → 取得内存首地址 → 解密并加载 → 释放模型句柄 的完整流程。
SDK 调用
cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
const char* encPath = "models/game.olam";
std::ifstream ifs(encPath, std::ios::binary | std::ios::ate);
if (!ifs) return;
long pkgSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> pkgBuf(pkgSize);
ifs.read((char*)pkgBuf.data(), pkgSize);
long handle = ola.YoloLoadModelMemory((long)pkgBuf.data(), pkgSize, "your_password", -1);
if (handle != 0) {
ola.YoloReleaseModel(handle);
}csharp
using System.IO;
using System.Runtime.InteropServices;
using OLAPlug;
var ola = new OLAPlugServer();
byte[] pkgBytes = File.ReadAllBytes(@"models\game.olam");
GCHandle pin = GCHandle.Alloc(pkgBytes, GCHandleType.Pinned);
try {
long handle = ola.YoloLoadModelMemory(
pin.AddrOfPinnedObject().ToInt64(), pkgBytes.Length, "your_password", -1);
if (handle != 0) ola.YoloReleaseModel(handle);
} finally { pin.Free(); }python
from OLAPlugServer import OLAPlugServer
import ctypes
ola = OLAPlugServer()
with open("models/game.olam", "rb") as f:
pkg_bytes = f.read()
buf = ctypes.create_string_buffer(pkg_bytes, len(pkg_bytes))
handle = ola.YoloLoadModelMemory(
ctypes.addressof(buf), len(pkg_bytes), "your_password", -1)
if handle:
ola.YoloReleaseModel(handle)java
import com.olaplug.OLAPlugServer;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import java.nio.file.Files;
import java.nio.file.Paths;
OLAPlugServer ola = new OLAPlugServer();
byte[] pkgBytes = Files.readAllBytes(Paths.get("models/game.olam"));
Memory mem = new Memory(pkgBytes.length);
mem.write(0, pkgBytes, 0, pkgBytes.length);
long handle = ola.YoloLoadModelMemory(
Pointer.nativeValue(mem), pkgBytes.length, "your_password", -1);
if (handle != 0) ola.YoloReleaseModel(handle);cpp
var ola = com("OlaPlug.OlaSoft")
var pkgSize = ola.GetFileSize("models/game.olam")
var pkgPtr = ola.ReadBytesFromFile("models/game.olam", 0, 0)
if(pkgPtr && pkgSize > 0) {
var handle = ola.YoloLoadModelMemory(pkgPtr, pkgSize, "your_password", -1)
ola.FreeMemoryPtr(pkgPtr)
if(handle) ola.YoloReleaseModel(handle)
}vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
pkgSize = ola.GetFileSize("models/game.olam")
pkgPtr = ola.ReadBytesFromFile("models/game.olam", 0, 0)
If pkgPtr <> 0 And pkgSize > 0 Then
handle = ola.YoloLoadModelMemory(pkgPtr, pkgSize, "your_password", -1)
ola.FreeMemoryPtr(pkgPtr)
If handle <> 0 Then ola.YoloReleaseModel(handle)
End Iftext
.局部变量 ola, OLAPlug
.局部变量 pkgData, 字节集
.局部变量 handle, 长整数型
ola.创建 ()
' ① 读入加密包到字节集
pkgData = 读入文件 (“models/game.olam”, )
.如果真 (取字节集长度 (pkgData) > 0)
' ② 取变量数据地址 → 内存首地址;取字节集长度 → size
handle = ola.YoloLoadModelMemory (
取变量数据地址 (pkgData), 取字节集长度 (pkgData), “your_password”, -1)
.如果真 (handle ≠ 0)
ola.YoloReleaseModel (handle)
.如果真结束
.如果真结束
' pkgData 在调用完成前须保持有效aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var pkgData = io.load("models/game.olam");
var handle = ola.YoloLoadModelMemory(pkgData, #pkgData, "your_password", -1);
if(handle) ola.YoloReleaseModel(handle);text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
变量 pkgData <类型 = 字节集类>
' ① 读入加密包
pkgData = 读入文件("models/game.olam", )
如果真 (取字节集长度(pkgData) > 0)
{
' ② 取字节集指针 → 内存首地址
长整数 handle = ola.YoloLoadModelMemory(
取字节集指针(pkgData), 取字节集长度(pkgData), "your_password", -1)
如果真 (handle ≠ 0)
{
ola.YoloReleaseModel(handle)
}
}cpp
#include "OLAPlugServer.h"
#include <fstream>
#include <vector>
OLAPlugServer ola;
std::ifstream ifs("models/game.olam", std::ios::binary | std::ios::ate);
long pkgSize = (long)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> pkgBuf(pkgSize);
ifs.read((char*)pkgBuf.data(), pkgSize);
long handle = ola.YoloLoadModelMemory((long)pkgBuf.data(), pkgSize, "your_password", -1);
if (handle != 0) ola.YoloReleaseModel(handle);原生 DLL 调用
cpp
#include <fstream>
#include <vector>
long instance = CreateCOLAPlugInterFace();
std::ifstream ifs("models/game.olam", std::ios::binary | std::ios::ate);
auto pkgSize = (int)ifs.tellg();
ifs.seekg(0);
std::vector<unsigned char> buf(pkgSize);
ifs.read((char*)buf.data(), pkgSize);
long handle = YoloLoadModelMemory(instance, (long)buf.data(), pkgSize, "your_password", -1);
if (handle != 0) YoloReleaseModel(instance, handle);csharp
using System.IO;
using System.Runtime.InteropServices;
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long YoloLoadModelMemory(long ola, long memoryAddr, int size, string password, int inferenceDevice);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int YoloReleaseModel(long ola, long handle);
long instance = CreateCOLAPlugInterFace();
byte[] pkgBytes = File.ReadAllBytes(@"models\game.olam");
GCHandle pin = GCHandle.Alloc(pkgBytes, GCHandleType.Pinned);
try {
long handle = YoloLoadModelMemory(instance,
pin.AddrOfPinnedObject().ToInt64(), pkgBytes.Length, "your_password", -1);
if (handle != 0) YoloReleaseModel(instance, handle);
} finally { pin.Free(); }python
from ctypes import CDLL, c_int, c_int64, create_string_buffer
ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
ola.YoloLoadModelMemory.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
with open("models/game.olam", "rb") as f:
pkg_bytes = f.read()
buf = create_string_buffer(pkg_bytes, len(pkg_bytes))
handle = ola.YoloLoadModelMemory(
instance, c_int64(id(buf.raw)), len(pkg_bytes), b"your_password", -1)
if handle:
ola.YoloReleaseModel(instance, handle)返回值
| 返回值 | 说明 |
|---|---|
| (返回值) | 长整数型:模型句柄,失败返回 0。 |
注意事项
| 项目 | 说明 |
|---|---|
| 模块权限 | 需要插件已开通 YOLO 模块权限(Reg、Login的FeatureList中包含YOLO特性)。 |
| 字节流 → 地址 | 易语言:取变量数据地址(字节集) + 取字节集长度(字节集);火山:取字节集指针(字节集) + 取字节集长度(字节集)。 |
| ReadBytesFromFile | 返回指针可直接作为 memoryAddr;加载成功后须 FreeMemoryPtr 释放读盘缓冲。 |
| 加密包格式 | 须为 YoloEncryptModel 生成的欧拉加密包(魔数 OLAYENC1)。 |
| 密码 | password 为 UTF-8 字符串,须与加密时一致。 |
| 释放 | 句柄不再使用时须 YoloReleaseModel 释放。 |
inferenceDevice | -1 表示 CPU;0 及以上表示 GPU 索引。GPU 不可用时可能自动回退 CPU(以实际 ExecutionProvider 为准)。 |
